package cn.gson.crm.control;




import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.gson.crm.model.pojo.ConnectorSelect;
import cn.gson.crm.model.pojo.ConnectorState;
import cn.gson.crm.model.pojoVo.ConnectorNeedData;
import cn.gson.crm.service.LLJ_ConnectorService;





@Controller
@SessionAttributes({"ConnectorId","listData","name"})         
public class LLJ_ConnectorController {
	@Autowired   
	private LLJ_ConnectorService llj_ConnectorService;
   
	   
	  
	
	@RequestMapping("to_demo")
	public String to_demo(){
		return "llj/Demo";      
	}
	
	@RequestMapping("/to_order")
	public String to_order(){
		return "llj/order";                           
	}
	
	@RequestMapping("/to_CustomerCareT")
	public String to_CustomerCareT(){
		return "llj/CustomerCareT";                            
	}
	
	@RequestMapping("/to_ConnectorSetUp")   
	public String to_ConnectorSetUp(){  
		System.out.println("进入to_ConnectorSetUp-----------------------");
		return "llj/ConnectorSetUp";            
	}
	
	@RequestMapping("/to_ConnectorSetUpT")   
	public String to_ConnectorSetUpT(){  
		System.out.println("进入to_ConnectorSetUp-----------------------");
		return "llj/ConnectorSetUpT";                
	}
	
	@RequestMapping("/to_ConnectorView")      
	public String to_ConnectorView(){         
		return "llj/ConnectorView";                    
	}
	
	@RequestMapping("/to_ConnectorSetUpThree")
	public String to_ConnectorSetUpThree(){
		return "llj/ConnectorSetUpThree";  
	} 
	@RequestMapping("/to_Cellediting")
	public String to_Cellediting(){
		return "llj/Cellediting";
	} 
	
	/* ConnectorState表   */
	/* 添加一条接件状况数据   */
	@GetMapping("/add_ConnectorData")
	@ResponseBody   
	public String add_ConnctorData(String name,Model model){ 
		System.out.println("--------------------");
		System.out.println("输出信息："+name);   
		if(llj_ConnectorService.addOneData(name)=="success"){     
			List<ConnectorState> list=llj_ConnectorService.findAll_connectorData();
			model.addAttribute("list_Connector", list);
			System.out.println("输出添加之后的查询信息："+list);
			return "success";    
		}else{
			System.out.println("单个接件状况自定义添加失败！！！");
			return "failed";          
		}   
	} 
	
	/* ConnectorState表   */
	/*  删除一条接件状况数据    */
	@RequestMapping("/delete_ConnectorData")
	@ResponseBody
	public String delete_ConnectorData(Long getID,Model model){
		//判断该记录是否有外键数据
		
		System.out.println("输出需要删除的id："+getID);         
		if(llj_ConnectorService.deleteData(getID)){
			List<ConnectorState> list=llj_ConnectorService.findAll_connectorData();  
			model.addAttribute("list_Connector", list);
			System.out.println("输出添加之后的查询信息："+list);   
			return "success";
		}else{
			System.out.println("删除单个接件状况失败！！！");
			return "false";
			
		}
		
	}
	    
	/* ConnectorState表   */
	/* 访问接件状态自定义页面 */
	/* 查询接件状况数据  */
	@GetMapping("/find_allConnector")    
	public String find_allConnector(Model model){
		List<ConnectorState> list=llj_ConnectorService.findAll_connectorData();
		model.addAttribute("list_Connector", list);  
		System.out.println("输出-------------------------------");
		System.out.println(list.get(0).getConnectorName());   
		return "llj/Connector";      
	}
	    
	/* ConnectorState表   */
	@GetMapping("/return_ConnectorPage")   
	public String return_ConnectorPage(Model model){   
		List<ConnectorState> list=llj_ConnectorService.findAll_connectorData();
		model.addAttribute("list_Connector", list);  
		return "llj/Connector";   
	}
	
	/* ConnectorSelect表   */ 
	/* 查找接件详情类型选择表数据   */ 
	@RequestMapping("/getAll_ConnectorSelect")  
	@ResponseBody
	public List<ConnectorSelect> getAll_ConnectorSelect(){   
		System.out.println("输出查询信息："+llj_ConnectorService.findAll_ConnectorSelect());
		
		return llj_ConnectorService.findAll_ConnectorSelect();    
	}  
	 
	/*  
	 * 
	 * */
	@RequestMapping("/Jump_ConnectorSetUpThree")
	public String Jump_ConnectorSetUpThree(Long connectorId,String name,Model model){
		model.addAttribute("ConnectorId", connectorId);
		model.addAttribute("name", name);    
		return "llj/ConnectorSetUpThree";     
		
	}  
	
	/* ConnectorState表
	 * ConnectorStateDetail表
	 * ConnectorSelect表
	 * 通过'接件状况表'id查询'接件状况详情表'和'接件详情类型选择表'数据
	 */
	@GetMapping("/getNeedData")     
	@ResponseBody       
	public List<ConnectorNeedData> getNeedData(Long connectorId,Model model) throws Exception{
		System.out.println(connectorId);
		//通过ID获得原始的POJO	获得list集合
		//实例化VO封装
		int num=llj_ConnectorService.getNeedData(connectorId).getConnectorStateDetails().size();
		List<ConnectorNeedData> listData=new ArrayList<ConnectorNeedData>();
		try {      
			ConnectorNeedData cn;       
			for(int i=0;i<num;i++){
				String Connector_detail_name=llj_ConnectorService.getNeedData(connectorId).getConnectorStateDetails().get(i).getConnectorDetailName();
				String Connector_select_getValue=llj_ConnectorService.getNeedData(connectorId).getConnectorStateDetails().get(i).getConnectorSelect().getConnectorSelectGetvalue();
				String Connector_select_setValue=llj_ConnectorService.getNeedData(connectorId).getConnectorStateDetails().get(i).getConnectorSelectSetvalue();
				cn=new ConnectorNeedData();
				cn.setConnector_detail_name(Connector_detail_name);
				cn.setConnector_select_getValue(Connector_select_getValue);
				cn.setConnector_select_setValue(Connector_select_setValue);  
				listData.add(cn);
			}
			
			model.addAttribute("listData", listData);  
			System.out.println("输出："+listData);  
			return listData;
		} catch (Exception e) {  
			return null;       
		}    
		                                      
	}
	
	
	
	
	
}
